package cn.stylefeng.guns.modular.leaveindex.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.modular.leaveindex.model.IndexDataResult;
import cn.stylefeng.guns.modular.leaveinfo.service.LeaveInfoService;
import cn.stylefeng.guns.modular.studentmanage.model.LeaveUserResult;
import cn.stylefeng.guns.modular.studentmanage.service.StuManageService;
import cn.stylefeng.guns.sys.modular.system.entity.FileTagPO;
import cn.stylefeng.guns.sys.modular.system.entity.LeaveInfoPO;
import cn.stylefeng.guns.sys.modular.system.entity.LeaveUserPO;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@Controller
@RequestMapping("/indexData")
public class IndexDataController {


    @Autowired
    private StuManageService stuManageService;

    @Autowired
    private LeaveInfoService leaveInfoService;

    /**
     * 初始化数据
     */
    @GetMapping("/init")
    @ResponseBody
    public ResponseData initData() {
        IndexDataResult indexDataResult = new IndexDataResult();
        //待审批记录
        indexDataResult.setPendingApprovals(getDataOfPendingApprovals());
        //已审批记录
        indexDataResult.setApprovaledRecords(getDataOfApprovaledRecords());
        //已拒绝记录
        indexDataResult.setRefuseRecords(getDataOfRefuseRecords());
        //已销假记录
        indexDataResult.setApprovalRecords(getDataOfApprovalRecords());
        //总人数
        indexDataResult.setTotalPeoples(getDataOfTotalPeoples());
        //在位人数
        indexDataResult.setIncumbentsPeoples(getDataOfTotalPeoples() - getDataOfLeavePeoples());
        //请假人数
        indexDataResult.setLeavePeoples(getDataOfLeavePeoples());
        return ResponseData.success(200, "查询成功", indexDataResult);
    }

    //待审批记录
    private int getDataOfPendingApprovals() {
        Long userId = LoginContextHolder.getContext().getUserId();
        List<LeaveInfoPO> indexFour = leaveInfoService.list(new QueryWrapper<LeaveInfoPO>().eq("leave_info_status", 0).and(wrapper -> wrapper
                .eq("stair_approval_id", userId).or().
                        eq("foxbase_approval_id", userId).or().
                        eq("tertiary_approval_id", userId)));
        if (!CollectionUtils.isEmpty(indexFour)) {
            return indexFour.size();
        }
        return 0;
    }

    //已审批记录
    private int getDataOfApprovaledRecords() {
        Long userId = LoginContextHolder.getContext().getUserId();
        List<LeaveInfoPO> indexFour = leaveInfoService.list(new QueryWrapper<LeaveInfoPO>().eq("leave_info_status", 1).and(wrapper -> wrapper
                .eq("stair_approval_id", userId).or().
                        eq("foxbase_approval_id", userId).or().
                        eq("tertiary_approval_id", userId)));
        if (!CollectionUtils.isEmpty(indexFour)) {
            return indexFour.size();
        }
        return 0;
    }

    //已拒绝记录
    private int getDataOfRefuseRecords() {
        Long userId = LoginContextHolder.getContext().getUserId();
        List<LeaveInfoPO> indexFour = leaveInfoService.list(new QueryWrapper<LeaveInfoPO>().eq("leave_info_status", 4).and(wrapper -> wrapper
                .eq("stair_approval_id", userId).or().
                        eq("foxbase_approval_id", userId).or().
                        eq("tertiary_approval_id", userId)));
        if (!CollectionUtils.isEmpty(indexFour)) {
            return indexFour.size();
        }
        return 0;
    }

    //已销假
    private int getDataOfApprovalRecords() {
        Long userId = LoginContextHolder.getContext().getUserId();
        List<LeaveInfoPO> indexFour = leaveInfoService.list(new QueryWrapper<LeaveInfoPO>().eq("leave_info_status", 6).and(wrapper -> wrapper
                .eq("stair_approval_id", userId).or().
                        eq("foxbase_approval_id", userId).or().
                        eq("tertiary_approval_id", userId)));
        if (!CollectionUtils.isEmpty(indexFour)) {
            return indexFour.size();
        }
        return 0;
    }

    //总人数
    private int getDataOfTotalPeoples() {
        Long userId = LoginContextHolder.getContext().getUserId();
        QueryWrapper<LeaveUserPO> leaveInfoPOQueryWrapper = new QueryWrapper<>();
        leaveInfoPOQueryWrapper.and(wrapper -> wrapper
                .eq("stair_approval_id", userId).or().
                        eq("foxbase_approval_id", userId).or().
                        eq("tertiary_approval_id", userId));
        List<LeaveUserPO> list = stuManageService.list(leaveInfoPOQueryWrapper);
        if (!CollectionUtils.isEmpty(list)) {
            return list.size();
        }
        return 0;
    }

    //请假人数
    private int getDataOfLeavePeoples() {
        //获取所有学生
        Long userId = LoginContextHolder.getContext().getUserId();
        QueryWrapper<LeaveUserPO> leaveInfoPOQueryWrapper = new QueryWrapper<>();
        leaveInfoPOQueryWrapper.and(wrapper -> wrapper
                .eq("stair_approval_id", userId).or().
                        eq("foxbase_approval_id", userId).or().
                        eq("tertiary_approval_id", userId));
        List<LeaveUserPO> userPOS = stuManageService.list(leaveInfoPOQueryWrapper);
        if (!CollectionUtils.isEmpty(userPOS)) {
            List<String> ids = userPOS.stream().map(LeaveUserPO::getLeaveUserNum).collect(Collectors.toList());
            //根据学号获取所有得请假信息
            List<LeaveInfoPO> leaveInfoPOS = leaveInfoService.list(new QueryWrapper<LeaveInfoPO>().in("leave_user_num", ids));
            //过滤请假状态为1--即已审批，则定义为不在位
            List<LeaveInfoPO> leavePeoples = leaveInfoPOS.stream().filter(leaveInfoPO -> leaveInfoPO.getLeaveInfoStatus() == 1).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(leavePeoples)) {
                return leavePeoples.size();
            }
        }
        return 0;
    }
}
